<template>
  <div class="app-container">
    <!--    0人工审核中，1审核通过，-1不通过，2未发布，3机器审核中-->
    <!--    <el-tabs v-model="status" @tab-click="handleClick">-->
    <!--      <el-tab-pane label="全部" name="-10"></el-tab-pane>-->
    <!--      <el-tab-pane label="人工审核中" name="0"></el-tab-pane>-->
    <!--      <el-tab-pane label="审核通过" name="1"></el-tab-pane>-->
    <!--      <el-tab-pane label="审核失败" name="-1"></el-tab-pane>-->
    <!--      <el-tab-pane label="未发布" name="2"></el-tab-pane>-->
    <!--      <el-tab-pane label="机器审核中" name="3"></el-tab-pane>-->
    <!--      <el-tab-pane label="已删除" name="4"></el-tab-pane>-->
    <!--    </el-tabs>-->
<!--    <span> 欢乐转盘</span>-->
<!--    <span> 本期已抽数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===1).lotteryCount }}-->
<!--    <span> 本期剩余数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===1).totalCycleCount }}-->

<!--    <span> 用户总消费:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===1).consumeNumber }}-->

<!--    <span> 礼物总价值:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===1).giftNumber }}-->

<!--    <span> 平台总盈利:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===1).consumeNumber-currentLotterys.find(c=>c.type===1).giftNumber }}-->
<!--    <br>-->
<!--    <span> 黄金转盘</span>-->
<!--    <span> 本期已抽数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===2).lotteryCount }}-->
<!--    <span> 本期剩余数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===2).totalCycleCount }}-->

<!--    <span> 用户总消费:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===2).consumeNumber }}-->

<!--    <span> 礼物总价值:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===2).giftNumber }}-->

<!--    <span> 平台总盈利:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===2).consumeNumber-currentLotterys.find(c=>c.type===2).giftNumber }}-->
<!--    <br>-->
<!--    <span> 初级盲盒</span>-->
<!--    <span> 本期已抽数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===3).lotteryCount }}-->
<!--    <span> 本期剩余数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===3).totalCycleCount }}-->

<!--    <span> 用户总消费:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===3).consumeNumber }}-->

<!--    <span> 礼物总价值:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===3).giftNumber }}-->

<!--    <span> 平台总盈利:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===3).consumeNumber-currentLotterys.find(c=>c.type===3).giftNumber }}-->
<!--    <br>-->
<!--    <span> 中级盲盒</span>-->
<!--    <span> 本期已抽数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===4).lotteryCount }}-->
<!--    <span> 本期剩余数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===4).totalCycleCount }}-->
<!--    <span> 用户总消费:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===4).consumeNumber }}-->

<!--    <span> 礼物总价值:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===4).giftNumber }}-->

<!--    <span> 平台总盈利:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===4).consumeNumber-currentLotterys.find(c=>c.type===4).giftNumber }}-->
<!--    <br>-->
<!--    <span> 高级盲盒</span>-->
<!--    <span> 本期已抽数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===5).lotteryCount }}-->
<!--    <span> 本期剩余数量:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===5).totalCycleCount }}-->

<!--    <span> 用户总消费:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===5).consumeNumber }}-->

<!--    <span> 礼物总价值:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===5).giftNumber }}-->

<!--    <span> 平台总盈利:</span>-->
<!--    {{ currentLotterys.find(c=>c.type===5).consumeNumber-currentLotterys.find(c=>c.type===5).giftNumber }}-->
<!--    <br>-->

    <span> 活动类型： </span>
    <template>
      <el-select v-model="type" placeholder="请选择类型" style="margin: 10px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </template>
    <el-button type="primary" round @click="fetchData(page); searchType=type">搜索</el-button>
    <el-button type="primary" plain @click="dialogCreateGiftVisible = true; giftDTO = {}">添加礼物</el-button>
    <el-button type="primary" plain @click="dialogFormCycleVisible = true; ">设置期数</el-button>

    <span> 礼物总价值:</span>
    {{list.reduce((accumulator, currentObject) => {
    const count = currentObject.count || 0;
    return accumulator + (currentObject.price * count)
  }, 0) }}

    <span> 礼物剩余价值:</span>
    {{list.reduce((accumulator, currentObject) => {
    const remainingQuantity = currentObject.remainingQuantity || 0;
    return accumulator + (currentObject.price * remainingQuantity)
  }, 0) }}
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="110">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="名称" width="150">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="图标" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.icon"
            :preview-src-list="[scope.row.icon]"
          />
        </template>
      </el-table-column>
      <el-table-column label="价格" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column v-if="searchType=='3' || searchType == '4' || giftDTO.crateGiftId != null" label="概率" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.probability }}%
        </template>
      </el-table-column>
<!--      <el-table-column label="本期剩余数量" width="110" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.row.remainingQuantity }}-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="每期数量" width="110" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.row.count }}-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="抽多少次可中" width="110" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.row.conditionCount }}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="排序" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.status }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.type===2" size="mini" type="primary" round @click="dialogFormViewItemVisible = true; getItemList(scope.row.id); ">
            查看奖池
          </el-button>

          <el-button size="mini" type="primary" round @click="dialogFormUpdateGiftVisible = true; updateGiftDTO = scope.row">
            修改礼物
          </el-button>

          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click=" handelDelete(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="奖品列表" :visible.sync="dialogFormViewItemVisible" width="76%">
      <span> 礼物总价值:</span>
      {{itemList.reduce((accumulator, currentObject) => {
      const count = currentObject.count || 0;
      return accumulator + (currentObject.price * count)
    }, 0) }}

      <span> 礼物剩余价值:</span>
      {{itemList.reduce((accumulator, currentObject) => {
      const remainingQuantity = currentObject.remainingQuantity || 0;
      return accumulator + (currentObject.price * remainingQuantity)
    }, 0) }}
      <el-button type="primary" plain @click="dialogCreateGiftVisible = true;">添加奖品</el-button>

      <el-table
        v-loading="listLoading"
        style="margin-top: 10px"
        :data="itemList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="ID" width="110">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>

        <el-table-column label="名称" width="150">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="图标" width="200" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.icon"
              :preview-src-list="[scope.row.icon]"
            />
          </template>
        </el-table-column>
        <el-table-column label="价格" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.price }}
          </template>
        </el-table-column>

        <el-table-column label="概率" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.probability }}%
          </template>
        </el-table-column>

<!--        <el-table-column label="本期剩余数量" width="110" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.remainingQuantity }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="每期数量" width="110" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.count }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="抽多少次可中" width="110" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.conditionCount }}-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="操作" width="110" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" round @click="dialogFormUpdateGiftVisible = true; updateGiftDTO = scope.row">
              修改奖品
            </el-button>
            <!--            <el-button-->
            <!--              size="mini"-->
            <!--              type="danger"-->
            <!--              icon="el-icon-delete"-->
            <!--              circle-->
            <!--              @click="handelDeleteTopic(scope.row.id)"-->
            <!--            />-->
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog title="修改礼物" :visible.sync="dialogFormUpdateGiftVisible" width="76%">
      <el-form ref="form" :model="updateGiftDTO" label-width="80px">
        <el-form-item label="礼物名称">
          <el-input v-model="updateGiftDTO.name" />
        </el-form-item>
        <el-form-item label="礼物价格">
          <el-input-number v-model="updateGiftDTO.price" :min="0.01" :max="100000000" label="描述文字" />
        </el-form-item>
<!--        <el-form-item label="每期最大数量">-->
<!--          <el-input-number v-model="updateGiftDTO.count" :min="0" :max="100000000" label="描述文字" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="抽多少次可中">-->
<!--          <el-input-number v-model="updateGiftDTO.conditionCount" :min="0" :max="100000000" label="描述文字" />-->
<!--        </el-form-item>-->
        <el-form-item v-if="searchType=='3' || searchType == '4' || giftDTO.crateGiftId != null" label="奖品概率">
          <el-input-number v-model="updateGiftDTO.probability" :min="0.01" :max="100000000" label="描述文字" />%
        </el-form-item>
        <el-form-item label="礼物图标">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :limit="1"

            :on-success="handleUpdateGiftPreview"
            :on-remove="handleRemoveUpdateGift"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog>
            <img width="100%" :src="updateGiftDTO.icon" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="礼物特效">
          <el-upload
            class="upload-demo"
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            :on-remove="handleRemove2"
            :on-success="handlePictureCardPreviewUpdate"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传 svga 文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormUpdateGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateGift">确 定</el-button>
      </div>
    </el-dialog>

<!--    <el-dialog title="设置期数" :visible.sync="dialogFormCycleVisible">-->
<!--      <el-form ref="form" :model="setCycleDTO" label-width="80px">-->
<!--        <el-form-item label="礼物类型">-->
<!--          <template>-->
<!--            <el-select v-model="setCycleDTO.type" placeholder="请选择类型" style="margin: 10px">-->
<!--              <el-option-->
<!--                v-for="item in lotteryOptions"-->
<!--                :key="item.value"-->
<!--                :label="item.label"-->
<!--                :value="item.value"-->
<!--              />-->
<!--            </el-select>-->
<!--          </template>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="期数">-->
<!--          <el-input v-model="setCycleDTO.count" />-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogFormCycleVisible = false">取 消</el-button>-->
<!--        <el-button type="primary" @click="handleSetCycle">确 定</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->

<!--    添加宝箱礼物-->
    <el-dialog title="创建礼物" :visible.sync="dialogCreateGiftVisible" width="76%">
      <el-form ref="form" :model="giftDTO" label-width="80px">
        <el-form-item label="礼物名称">
          <el-input v-model="giftDTO.name" />
        </el-form-item>
        <el-form-item label="礼物价格">
          <el-input-number v-model="giftDTO.price" :min="0.01" :max="100000000" label="描述文字" />
        </el-form-item>
        <el-form-item v-if="giftDTO.crateGiftId != null" label="奖品概率">
          <el-input-number v-model="giftDTO.probability" :min="0.01" :max="100000000" label="描述文字" />%
        </el-form-item>
        <el-form-item label="是否上架">
          <el-switch
            v-model="giftDTO.status"
          />
        </el-form-item>
        <el-form-item v-if="giftDTO.crateGiftId == null" label="礼物类型">
          <el-select v-model="giftDTO.type" placeholder="礼物类型" style="margin: 10px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="礼物图标">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :limit="1"

            :on-success="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog >
            <img width="100%" :src="giftDTO.icon" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="礼物特效">
          <el-upload
            class="upload-demo"
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            :on-remove="handleRemove2"
            :on-success="handlePictureCardPreview2"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传 svga 文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="giftDTO.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCreateGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddGfit">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { getList, deleteById, addGift, updateGift, getItemList,setCycle } from '@/api/gift'
import { getCurrentLottery } from '@/api/party'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogFormViewItemVisible: false,
      options: [{
        value: '1',
        label: '经典'
      }, {
        value: '2',
        label: '盲盒'
      }, {
        value: '4',
        label: '欢乐转盘'
      }, {
        value: '3',
        label: '黄金转盘'
      }, {
        value: '5',
        label: '星际巡航'
      }, {
        value: '6',
        label: '美食大排档'
      }],
      lotteryOptions: [{
        value: '1',
        label: '欢乐转盘'
      }, {
        value: '2',
        label: '黄金转盘'
      }, {
        value: '3',
        label: '初级宝箱'
      }, {
        value: '4',
        label: '中级宝箱'
      }, {
        value: '5',
        label: '高级宝箱'
      }],
      fileList: [],
      itemList: [],
      list: null,
      searchType: '1',
      listLoading: true,
      status: '-10',
      page: 1,
      size: 10,
      total: 0,
      viewItem: {
        giftId: null
      },
      giftDTO: {
        crateGiftId: null,
        name: '',
        circleType: '0',
        sort: 0,
        status: false,
        icon: '',
        price: '',
        materialUrl: ''
      },
      setCycleDTO: {},
      currentLotterys: [],
      type: '1',
      updateGiftDTO: {
        id: null,
        probability: null,
        price: null,
        count: null,
        conditionCount: null,
        icon: '',
        materialUrl: ''
      },
      dialogFormUpdateGiftVisible: false,
      dialogCreateGiftVisible: false,
      dialogFormCycleVisible: false
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handlePictureCardPreview2(response, file) {
      this.giftDTO.materialUrl = response.data.url
      console.log(this.giftDTO.materialUrl)
    },
    handleSetCycle(){

    },
    handlePictureCardPreviewUpdate(response, file) {
      this.updateGiftDTO.materialUrl = response.data.url
    },
    handlePictureCardPreview(response, file) {
      this.giftDTO.icon = response.data.url
    },
    handleUpdateGiftPreview(response, file) {
      this.updateGiftDTO.icon = response.data.url
    },
    handleRemove2(file, fileList) {
      this.fileList = []
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    handleRemove(file) {
      this.giftDTO.icon = ''
      console.log(file)
    },
    handleRemoveUpdateGift(file) {
      this.updateGiftDTO.icon = ''
      console.log(file)
    },
    handleClick(tab, event) {
      this.fetchData(this.page)
    },

    handleUpdateGift() {
      this.dialogFormUpdateGiftVisible = false
      updateGift(this.updateGiftDTO).then(res => {
        console.log(res)
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      })
    },
    getCurrentLottery() {
      getCurrentLottery().then(res => {
        this.currentLotterys = res.data
      })
    },
    getItemList(val) {
      this.giftDTO.crateGiftId = val
      getItemList({ giftId: val }).then(res => {
        this.itemList = res.data
      })
    },
    handleAddGfit() {
      this.dialogCreateGiftVisible = false
      this.fileList = []
      if (this.giftDTO.status == null){
        this.giftDTO.status = false
      }
      addGift(this.giftDTO).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '创建成功!'
        })
        // if (this.giftDTO.crateGiftId != null) {
        //   console.log(this.giftDTO.crateGiftId)
        //   this.getItemList(this.giftDTO.crateGiftId)
        // }
      })
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      this.getCurrentLottery()
      getList({ page: this.page, size: this.size, type: this.type }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该礼物, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
